package com.winchain.chainbill.serfee.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.file.po.PubFile;
import com.shell.bee.file.service.PubFileDlService;
import com.shell.bee.file.service.PubFileService;
import com.shell.bee.file.utils.CommonFileUtil;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.report.utils.FileEncryptUtil;
import com.shell.bee.report.utils.PdfUtils;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.service.CbAttService;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.serfee.service.ServiceFeeService;
import com.winchain.chainbill.serfee.vo.SerFeeVo;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.service.TbDctService;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ServiceFeeController {
	@Autowired
	private ServiceFeeService serviceFeeService;
	@Autowired
	private CbAttService cbAttService;
	@Autowired
	private CbDctService cbDctService;
	@Autowired
	private TbDctService tbDctService;
	@Autowired
	private ContService contService;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private PubFileService pubFileService;
	@Autowired
	private PubFileDlService pubFileDlService;
	private String textMark = "������%s����������������������������������������";

	@RequestMapping
	@ResponseBody
	@Logging(module = "����������", methods = "��������������")
	public List<SerFeeVo> queryServiceFeePayConfirmList(PageLimit limit, String bizType, String cusName) {
		return this.serviceFeeService.queryServiceFeePayConfirmList(limit, bizType, cusName);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "����������", methods = "��������")
	public List<SerFeeVo> queryServiceFeePaySuccessList(PageLimit limit, String bizType, String cusName) {
		return this.serviceFeeService.queryServiceFeePaySuccessList(limit, bizType, cusName);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "����������", methods = "����")
	public SerFeeVo detail(String dctId, String bizType) {
		SerFeeVo serFeeVo = this.serviceFeeService.detail(dctId, bizType);
		this.cbAttService.setSerFeeTbAtt(serFeeVo);
		return serFeeVo;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "����������", methods = "����")
	public RetMsg success(String dctId, String bizType) {
		return this.serviceFeeService.success(dctId, bizType);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "����������", methods = "����")
	public RetMsg fail(String dctId, String bizType, String remark) {
		return this.serviceFeeService.fail(dctId, bizType, remark);
	}

	@RequestMapping
	@Logging(module = "����������", methods = "����������������")
	public void getDctDebitFile(String dctId, String bizType, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CusRec artisan = this.cusRecService.getArtisan(null);
		ContBas cont;
		if ("0".equals(bizType)) {
			CbDct dct = (CbDct) this.cbDctService.selectByKey(dctId);
			cont = this.contService.getLatestCont(artisan.getCusId(), dct.getBillId(), "18", null);
		} else {
			TbDct dct = (TbDct) this.tbDctService.selectByKey(dctId);
			cont = this.contService.getLatestCont(artisan.getCusId(), dct.getBillId(), "54", null);
		}
		ContProt prot = this.contService.getProt(cont.getContId());
		CommonFileUtil.writeFileToClient(prot.getProtTitle() + ".pdf", new FileInputStream(new File(prot.getProtLoc())),
				response, request);
	}

	@RequestMapping
	@Logging(module = "����������", methods = "������������")
	public void getDctDebitDetailList(String dctId, String bizType, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String billId;
		if ("0".equals(bizType)) {
			CbDct dct = (CbDct) this.cbDctService.selectByKey(dctId);
			billId = dct.getBillId();
		} else {
			TbDct dct = (TbDct) this.tbDctService.selectByKey(dctId);
			billId = dct.getBillId();
		}
		PubFile file = new PubFile();
		file.setRelateId(billId);
		file.setFileType("12");
		List<PubFile> files = this.pubFileService.selectFiles(file);
		if ((files == null) || (files.size() == 0)) {
			return;
		}
		file = (PubFile) files.get(0);
		CommonFileUtil.writeFileToClient(file.getFileName() + "." + "pdf",
				new FileInputStream(new File(file.getFilePath())), response, request);
	}

	@RequestMapping
	@Logging(module = "����������", methods = "������������")
	public void getDctDebitCert(String dctId, String bizType, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String billId;
		if ("0".equals(bizType)) {
			CbDct dct = (CbDct) this.cbDctService.selectByKey(dctId);
			billId = dct.getBillId();
		} else {
			TbDct dct = (TbDct) this.tbDctService.selectByKey(dctId);
			billId = dct.getBillId();
		}
		PubFile file = new PubFile();
		file.setRelateId(billId);
		file.setFileType("13");
		List<PubFile> files = this.pubFileService.selectFiles(file);
		if ((files == null) || (files.size() == 0)) {
			file = this.serviceFeeService.genDebitCert(dctId, bizType);
		} else {
			file = (PubFile) files.get(0);
		}
		CommonFileUtil.writeFileToClient(file.getFileName() + "." + "pdf",
				new FileInputStream(new File(file.getFilePath())), response, request);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "����������", methods = "������������")
	public boolean existDctDebitCert(String billId) {
		PubFile file = new PubFile();
		file.setRelateId(billId);
		file.setFileType("13");
		List<PubFile> files = this.pubFileService.selectFiles(file);
		if ((files == null) || (files.size() == 0)) {
			return false;
		}
		return true;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "����������", methods = "������������")
	public String openDctDebitCert(String billId, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PubFile file = new PubFile();
		file.setRelateId(billId);
		file.setFileType("14");
		List<PubFile> files = this.pubFileService.selectFiles(file);
		if ((files == null) || (files.size() == 0)) {
			file = new PubFile();
			file.setRelateId(billId);
			file.setFileType("13");
			files = this.pubFileService.selectFiles(file);
			if ((files == null) || (files.size() == 0)) {
				return "";
			}
			file = (PubFile) files.get(0);
			String pdfPath = file.getFilePath();
			String pngPath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".png";
			PdfUtils.pdf2Png(file.getFilePath(), pngPath);
			String pngFileId = SystemUtil.getUUID();
			file = this.pubFileService.saveFile(pngFileId, file.getFileName(), pngPath, "14");

			this.pubFileService.setRelateId(pngFileId, billId, "14");
		} else {
			file = (PubFile) files.get(0);
		}
		String pngBase64 = "data:image/png;base64," + FileEncryptUtil.getImageBase64(file.getFilePath());
		return pngBase64;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "����������", methods = "������������")
	public String printDctDebitCert(String billId, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PubFile file = new PubFile();
		file.setRelateId(billId);
		file.setFileType("13");
		List<PubFile> files = this.pubFileService.selectFiles(file);
		if ((files == null) || (files.size() == 0)) {
			return "";
		}
		file = (PubFile) files.get(0);

		String downloader = CommonDataUtil.getUserId();
		int downloadCount = this.pubFileDlService.getDownloadCount(file.getFileId());

		downloadCount++;
		String text = String.format(this.textMark, new Object[] { Integer.valueOf(downloadCount) });
		String pngPath = file.getFilePath();
		String pngFileId = SystemUtil.getUUID();
		String newPngPath = pngPath.substring(0, pngPath.lastIndexOf(File.separator)) + File.separator + pngFileId
				+ ".png";

		PdfUtils.pdf2Png(file.getFilePath(), newPngPath);
		PdfUtils.createStringMark(newPngPath, text, newPngPath);

		PubFile pngFile = this.pubFileService.saveFile(pngFileId, file.getFileName(), newPngPath, "14");

		this.pubFileService.setRelateId(pngFileId, billId, "14");

		this.pubFileDlService.download(file.getFileId(), downloader);

		String pngBase64 = FileEncryptUtil.getImageBase64(pngFile.getFilePath());
		return "data:image/png;base64," + pngBase64;
	}
}
